<?php
$adm_menu = "1";

include("config.php");
if (!isset($_SESSION["auser"]) || $_SESSION["auser"] == "" || $_SESSION['agroup'] != "WEB_ADMIN") {
header('Location: index.php');
die();
}


$db_www = new DB($server_username,$server_pass,$server_db,$server,$driver);
$db_rdata = new DB($rdata_server_username,$rdata_server_pass,$rdata_server_db,$rdata_server,$rdata_driver);

$err = "";

if (isset($_REQUEST["findim"])){
	if ($_REQUEST["findim"] == "1") {
		$test_q = "SELECT TOP 1 N08_kodas_ks,N08_PAV FROM n08_klij WHERE N08_kodas_ks = '".addslashes_mssql($_REQUEST["imcode"])."'";

		$im = $db_www->returnInfo($test_q);	
		if ($im == null) {
			$impav = "";
			
			$err .= "<br>Nerasta įmonė su tokiu kodu!";
		} else {
			$impav = iconv('windows-1257','UTF-8', $im["N08_PAV"]);
			
		}
	} else if ($_REQUEST["findim"] == "2") {
		$test_q = "SELECT TOP 1 n17_kodas_ps,N17_PAV FROM n17_prod WHERE n17_kodas_ps = '".addslashes_mssql($_REQUEST["prcode"])."'";
		$pr = $db_www->returnInfo($test_q);	
		if ($pr == null) {
			$test_q = "SELECT TOP 1 N25_KODAS_SS,N25_PAV FROM N25_KOD WHERE N25_KODAS_SS = '".addslashes_mssql($_REQUEST["prcode"])."'";
			$pr = $db_www->returnInfo($test_q);	
			if ($pr == null) {		
				$prpav = "";				
				$err .= "<br>Nerastas priedas su tokiu kodu!";
			} else {
				$prpav = iconv('windows-1257','UTF-8', $pr["N25_PAV"]);				
			}				
		} else {
			$prpav = iconv('windows-1257','UTF-8', $pr["N17_PAV"]);
			
		}
	}
}


if (isset($_REQUEST["act"]) && !isset($_REQUEST["findim"])){
	 if ($_REQUEST["act"] == "save") {		

			//ar yra toks imones kodas
		$test_q = "SELECT N08_kodas_ks,N08_PAV FROM n08_klij WHERE N08_kodas_ks = '".$_REQUEST["imcode"]."'";
		$im = $db_www->returnInfo($test_q);		
		if (empty($im)) {
			$err .= "<br>Nerasta įmonė su tokiu kodu!";
		} else {
			$impav=iconv('windows-1257','UTF-8', $im["N08_PAV"]);
		}
		$test_q = "SELECT TOP 1 n17_kodas_ps,N17_PAV FROM n17_prod WHERE n17_kodas_ps = '".addslashes_mssql($_REQUEST["prcode"])."'";
		$pr = $db_www->returnInfo($test_q);	
		if ($pr == null) {
			$test_q = "SELECT TOP 1 n25_kodas_bs,N25_PAV FROM N25_KOD WHERE n25_kodas_bs = '".addslashes_mssql($_REQUEST["prcode"])."'";
			$pr = $db_www->returnInfo($test_q);	
			if ($pr == null) {								
				$err .= "<br>Nerastas priedas su tokiu kodu!";
			} else {
				$prpav =  iconv('windows-1257','UTF-8', $pr["N25_PAV"]);	
			}			
		}  else {
			$prpav = iconv('windows-1257','UTF-8', $pr["N17_PAV"]);
		} 
	 
		if (isset($_REQUEST["code"])) { //redagavimas
			//jei nebuvo klaidu
			if ($err == ""){		
				$sql = "exec web_update_register
				@id='". addslashes_mssql($_REQUEST["code"])."',
				@eil_nr='". addslashes_mssql($_REQUEST["eil_nr"])."',
				@imcode='". addslashes_mssql($_REQUEST["imcode"])."'
				,@impav='". addslashes_mssql(ltCharsToEntities($impav))
				."',@prcode='".addslashes_mssql($_REQUEST["prcode"])."'
				,@prpav='".addslashes_mssql(ltCharsToEntities($prpav))				
				."',@tipas='".addslashes_mssql($_REQUEST["tipas"])."';";
				$db_rdata->execute($sql);
				
				header('Location: register.php');
			}			
		} else {
			die("nerastas irasas");
		}
	}
}	
include("header.php");
include("menu.php");


?>




<?php



	if (isset($_REQUEST["code"])) {
		$code = $_REQUEST["code"];

		$sql = str_replace("<kodas>", addslashes_mssql($code), $reginfo_query);
		$u = $db_rdata->returnInfo($sql);
		if (!isset($_REQUEST["imcode"])) {
			$imcode = trim($u["N90_kodas_ks"]);	
			$impav = trim(ltCharsFromEntities($u["N90_pav"]));	
			$prcode = trim($u["N90_kodas"]);	
			$prpav = trim(ltCharsFromEntities($u["N90_pav1"]));	
			$tipas = trim($u["N90_tipas"]);	
			$eil_nr = trim($u["N90_eil_nr"]);			
		} else {
			$imcode = $_REQUEST["imcode"];	
			$impav = $_REQUEST["impav"];	
			$prcode = $_REQUEST["prcode"];	
			$prpav = $_REQUEST["prpav"];
			$tipas = $_REQUEST["tipas"];
			$eil_nr = $_REQUEST["eil_nr"];
		}
		
	

	
	} 
	



?>
<script>
function delRecord(id){
	var answer = confirm("Ar ištrinti įrašą?");
	if (answer){
		window.location='register.php?act=del&id='+id;
	}

}
</script>
<form method="post" name='forma' action='reg.php?act=save'>
<input id="code" type="hidden" name="code" value="<?php echo $code?>">
<table class="innercont" cellspacing="0" cellpadding="10" border="0">
<tr><td class="leftcol2">
<input class='but bgwhite nopad' type = "submit" value = "Išsaugoti"><br><br>
<Button id='unblockButton' class='but bgwhite nopad' onclick="window.location='register.php';return false;">Atgal</Button><br><br>
<Button id='unblockButton' class='but bgwhite nopad' onclick="delRecord('<?php echo $code; ?>');return false;">Trinti</Button>

</td><td class="centercol">
<?php
if ($err != ""){
	echo "<p style='background-color:transparent;color:red;font-weight:bold;font-size:10px;'>".$err."</p>";
}
?>
<div class="title2">Registro įrašo koregavimas</div>
<table width="100%" border="0">
<tr><td valign="top" class="fieldtitle">Kliento kodas:</td><td valign="top" colspan="2" class="fieldtitle">Kliento pavadinimas:</td></tr>
<tr><td valign="top">
<input id='imcode' class="inp" type='text' name="imcode" value="<?php echo htmlspecialchars($imcode) ?>"><br>
<div style="float:right;"><A class='flink' href="#" onclick='document.forma.action="reg.php?act=save&findim=1";document.forma.submit()'>Ieškoti</A></div>
</td><td valign="top" colspan="2">
<input style="width:275px;" id='impav' class="inp" type='text' name="impav" value="<?php echo htmlspecialchars($impav, ENT_QUOTES)?>" readonly="readonly">
</td></tr>
<tr><td valign="top" class="fieldtitle">Priedo kodas:</td><td valign="top" colspan="2" class="fieldtitle">Priedo pavadinimas:</td></tr>
<tr><td valign="top">
<input id='prcode' class="inp" type='text' name="prcode" value="<?php echo htmlspecialchars($prcode) ?>"><br>
<div style="float:right;"><A class='flink' href="#" onclick='document.forma.action="reg.php?act=save&findim=2";document.forma.submit()'>Ieškoti</A></div>
</td><td valign="top" colspan="2">
<input style="width:275px;" id='impav' class="inp" type='text' name="prpav" value="<?php echo htmlspecialchars($prpav, ENT_QUOTES)?>" readonly="readonly">
</td></tr>
<tr><td valign="top" class="fieldtitle">Tipas:</td><td valign="top" colspan="2" class="fieldtitle">Eilės numeris:</td></tr>
<tr><td valign="top">
<select name="tipas" class="select" >
<option value="1" <?php if ($tipas=="1") echo "SELECTED";?>>Pirkimas</option>
<option value="2" <?php if ($tipas=="2") echo "SELECTED";?>>Pardavimas</option>
</select>
</td>
<td valign="top"><input id="eil_nr" class="inp" type='text' name="eil_nr" value="<?php echo htmlspecialchars($eil_nr, ENT_QUOTES) ?>"><br>
	</td><td valign="top"></td></tr>


<tr><td colspan="3">&nbsp;</td></tr>
<tr><td colspan="3">&nbsp;</td></tr>
<tr><td colspan="3">


</td></tr>
</table>

</td><td class="rightcol">


</td></tr>
<tr><td>&nbsp;</td><td colspan="2">

</td></tr>
</table>
</form>
<?php
include("footer.php");
?>